package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Log;
import com.sofwin.base.ResponseEntity;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.MenuService;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @packageName: com.sofwin.controller
 * @author: andyliu
 * @date: 2022/8/4 16:33
 * @email 115176513@qq.com
 * @description: TODO
 */

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping("toList")
    public String toList(){
        return "user/list";
    }
    @GetMapping("toList2")
    public String toList2(Integer roleId,Model model,Integer id){
            model.addAttribute("roleId",roleId);
            model.addAttribute("id",id);
        return "user/list2";
    }

    @GetMapping("toEdit")
    public String toEdit(Integer id, Model model)
    {
        if(id!=null){
            model.addAttribute("user",userService.queryById(id));
        }
        model.addAttribute("depts",null);
        return "user/edit";
    }

    @GetMapping()
    @ResponseBody
    @Log(module = "用户数据查询",operatorType =1 )
    public ResponseEntity data(SysUser user,Integer roleId, @RequestParam(name = "pageNumber",required = true,defaultValue = "1") Integer pageNumber,
                               @RequestParam(name="pageSize",required = true,defaultValue = "10") Integer pageSize){
        PageInfo<SysUser> page = userService.queryByPage(user,roleId, pageNumber, pageSize);
        return ResponseEntity.success(page);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    @Log(module = "根据id查询用户",operatorType =1 )
    public ResponseEntity info(@PathVariable Integer id){
        SysUser user = userService.queryById(id);
        return ResponseEntity.success(user);
    }

    @PostMapping("/save")
    @ResponseBody
    @Log(module = "用户保存",operatorType =0 )
    public ResponseEntity save(SysUser user){
        boolean status = userService.saveUser(user);
        if(status){
            return ResponseEntity.success();
        }
        return ResponseEntity.error();
    }
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @Log(module = "根据ID删除用户信息",operatorType =3 )
    public ResponseEntity delete(@PathVariable Integer id){
        boolean status = userService.removeUserById(id);
        if(status){
            return ResponseEntity.success();
        }
        return  ResponseEntity.error();
    }
    @GetMapping("/deletes")
    @ResponseBody
    public ResponseEntity deletes(@RequestParam("ids[]") Integer[] ids){
        boolean status = userService.removeUserByIds(ids);
        if(status){
            return ResponseEntity.success();
        }
        return  ResponseEntity.error();
    }
    @GetMapping("/test")
    @ResponseBody
    public ResponseEntity test(){
        List<SysUser> users = new ArrayList<>();
        for(int i=0;i<100;i++){
            SysUser user = new SysUser();
            user.setUserName("admin"+i);
            user.setId(i);
            user.setPwd("admin"+i);
            users.add(user);
        }
        return ResponseEntity.success(users);
    }
       



}
